package algame;

import algame.Texture;
import algame.TextureLoader;
import algame.MeshLoader;

import java.lang.Math;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

public class Game {

	private float rot = 0;
	private float camx = 0;
	private float herox = 0;
	private float heroz = 0;

	private int old_oggetto_sel = -1;
	private int oggetto_sel = -1;

	//private Instance instances[];
	//private int totinstance = 0;

	private String oggetti[] = new String[10];
	private float oggettix[] = new float[10];
	private float oggettiy[] = new float[10];
	private float oggettiz[] = new float[10];
	private int totoggetti = 0;

	private String inventario[] = new String[10];
	private int totinventario = 0;

	private TextureLoader textureLoader;
	private Texture tex;
	private Font font;


	public void addOggetto(String code, float x, float y, float z) {
		oggetti[totoggetti] = code;
		oggettix[totoggetti] = x;
		oggettiy[totoggetti] = y;
		oggettiz[totoggetti] = z;
		totoggetti += 1;
	}

	public void gameInit() {

		// init OpenGL
		GL11.glViewport(0,0,800,600);
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GLU.gluPerspective(45.0f, (float) 800/ (float) 600,0.1f,100.0f);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		GL11.glLoadIdentity();
		GL11.glClearColor(0.2f,0.2f,0.2f,0.2f);
		GL11.glClearDepth(1.0f);
		GL11.glEnable(GL11.GL_DEPTH_TEST);
		GL11.glDepthFunc(GL11.GL_LEQUAL);
		GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
		GL11.glDisable(GL11.GL_LIGHTING);

		GL11.glEnable(GL11.GL_TEXTURE_2D);
//		GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);

//		carica texture

		textureLoader = new TextureLoader();

    try {
			tex = textureLoader.getTexture("res/algame.png");
    } catch (Exception e) {
    	e.printStackTrace();
      System.exit(0);
    }


		addOggetto("door_dn|ads", 0.1f, 0f, -2.0f);
		addOggetto("door_dx|sjs", 2.0f, 0f, 1.4f);
		addOggetto("door_sx|b", -2.0f, 0f, 0f);
		addOggetto("door_up|lolol", 1.7f, 0f, 2.0f);
		addOggetto("key|abc", (float) Math.random() * 2, 0f, (float) Math.random() *2);
		addOggetto("key|abc", (float) Math.random() * 2, 0f, (float) Math.random() *2);
		addOggetto("key|abc", (float) Math.random() * 2, 0f, (float) Math.random() *2);
		addOggetto("key|abc", (float) Math.random() * 2, 0f, (float) Math.random() *2);
		addOggetto("key|abc", (float) Math.random() * 2, 0f, (float) Math.random() *2);
		addOggetto("ba", (float) Math.random() * 2, 0f, (float) Math.random() *2);

		MeshLoader.load("res/yuredd.obj","all");

		MeshLoader.spos(1.8f, 0f, 0f);
		MeshLoader.scol(1,1,1);
		MeshLoader.ssca(0.1f,0.1f,0.1f);
	}

	public void gameLogic() {

		rot = rot + 0.1f;

		MeshLoader.srot(rot, 0, 1, 0);

		if(Keyboard.isKeyDown(Keyboard.KEY_A) && herox > -1.8f) { herox-=0.002f; }
		if(Keyboard.isKeyDown(Keyboard.KEY_D) && herox < 1.8f) { herox+=0.002f; }
		if(Keyboard.isKeyDown(Keyboard.KEY_W) && heroz > -1.8f) { heroz-=0.002f; }
		if(Keyboard.isKeyDown(Keyboard.KEY_S) && heroz < 1.8f) { heroz+=0.002f; }
		oggetto_sel = -1;
		for(int i = 0; i < totoggetti; i++) {
			if(Math.abs(herox - oggettix[i]) < 0.2 && Math.abs(heroz - oggettiz[i]) < 0.2) {
				oggetto_sel = i;
			}
		}
		if(oggetto_sel != old_oggetto_sel) {
			if(oggetto_sel == -1) {
				System.out.println("Selezione: Niente");
			} else {
				System.out.println("Selezione: " + oggetti[oggetto_sel]);
			}
		}
		old_oggetto_sel = oggetto_sel;
	}

	public void gameGraphics() {
		// Clear the screen and depth buffer
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glLoadIdentity();

		GL11.glTranslatef(camx,-1.0f,-6.0f);
		GL11.glEnable(GL11.GL_TEXTURE_2D);

		MeshLoader.render(0);

				tex.bind();

//pavimento
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glColor3f(0.5f,0.5f,1.0f);
			GL11.glTexCoord2f(0, 0);
		GL11.glVertex3f(-2.0f, 0.0f, -2.0f);
			GL11.glTexCoord2f(tex.getWidth(), 0);
		GL11.glVertex3f(2.0f, 0.0f, -2.0f);
		GL11.glColor3f(4.0f,0.5f,1.0f);
			GL11.glTexCoord2f(tex.getWidth(), tex.getHeight());
		GL11.glVertex3f(2.0f, 0.0f, 2.0f);
			GL11.glTexCoord2f(0, tex.getHeight());
		GL11.glVertex3f(-2.0f, 0.0f, 2.0f);

//muro dx
			GL11.glTexCoord2f(tex.getWidth(), tex.getHeight());
		GL11.glVertex3f(2.0f, 0.0f, 2.0f);
			GL11.glTexCoord2f(0, tex.getHeight());
		GL11.glVertex3f(2.0f, 0.0f, -2.0f);
			GL11.glTexCoord2f(0, 0);
		GL11.glVertex3f(2.0f, 0.6f, -2.0f);
			GL11.glTexCoord2f(tex.getWidth(), 0);
		GL11.glVertex3f(2.0f, 0.6f, 2.0f);

//muro sx
			GL11.glTexCoord2f(tex.getWidth(), tex.getHeight());
		GL11.glVertex3f(-2.0f, 0.0f, -2.0f);
			GL11.glTexCoord2f(0, tex.getHeight());
		GL11.glVertex3f(-2.0f, 0.0f, 2.0f);
			GL11.glTexCoord2f(0, 0);
		GL11.glVertex3f(-2.0f, 0.6f, 2.0f);
			GL11.glTexCoord2f(tex.getWidth(), 0);
		GL11.glVertex3f(-2.0f, 0.6f, -2.0f);

//muro dietro
			GL11.glTexCoord2f(0, tex.getHeight());
		GL11.glVertex3f(-2.0f, 0.0f, -2.0f);
			GL11.glTexCoord2f(0, 0);
		GL11.glVertex3f(-2.0f, 0.6f, -2.0f);
			GL11.glTexCoord2f(tex.getWidth(), 0);
		GL11.glVertex3f(2.0f, 0.6f, -2.0f);
			GL11.glTexCoord2f(tex.getWidth(), tex.getHeight());
		GL11.glVertex3f(2.0f, 0.0f, -2.0f);
		GL11.glEnd();

//eroe
		GL11.glPushMatrix();
		GL11.glDisable(GL11.GL_TEXTURE_2D);

		GL11.glTranslatef(herox,0.0f,heroz);

		GL11.glColor3f(0.8f,0.8f,1.2f);

		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex3f(-0.1f, 0.0f, 0.0f);
		GL11.glVertex3f(0.1f, 0.0f, 0.0f);
		GL11.glVertex3f(0.1f, 0.4f, 0.0f);
		GL11.glVertex3f(-0.1f, 0.4f, 0.0f);

		GL11.glVertex3f(0f, 0.0f, -0.1f);
		GL11.glVertex3f(0f, 0.0f, 0.1f);
		GL11.glVertex3f(0f, 0.4f, 0.1f);
		GL11.glVertex3f(0f, 0.4f, -0.1f);
		GL11.glEnd();
		GL11.glEnable(GL11.GL_TEXTURE_2D);

		GL11.glPopMatrix();

//oggetti

		for(int i = 0; i < totoggetti; i++) {
			GL11.glPushMatrix();
			GL11.glTranslatef(oggettix[i],oggettiy[i],oggettiz[i]);
			String codice[] = oggetti[i].split("\\|");
			if(oggetti[i] == "") {
				GL11.glColor3f(1f,1f,1f);
			} else {
				GL11.glColor3f(0f,1f,0f);
			}
			if(codice[0].equals("door_dx")) {
		GL11.glEnable(GL11.GL_TEXTURE_2D);
				GL11.glColor3f(0f,0f,1f);
				GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(0, 0);
				GL11.glVertex3f(0f, 0.0f, -0.2f);
			GL11.glTexCoord2f(0, tex.getHeight());
				GL11.glVertex3f(0f, 0.0f, 0.2f);
			GL11.glTexCoord2f(tex.getWidth(), tex.getHeight());
				GL11.glVertex3f(0f, 0.5f, 0.2f);
			GL11.glTexCoord2f(tex.getWidth(), 0);
				GL11.glVertex3f(0f, 0.5f, -0.2f);
				GL11.glEnd();
		GL11.glDisable(GL11.GL_TEXTURE_2D);

				GL11.glBegin(GL11.GL_TRIANGLES);
				GL11.glVertex3f(0f, 0.0f, -0.2f);
				GL11.glVertex3f(0f, 0.0f, 0.2f);
				GL11.glVertex3f(-0.1f, 0f, 0.0f);
				GL11.glEnd();
			} else if(codice[0].equals("door_sx")) {
		GL11.glDisable(GL11.GL_TEXTURE_2D);
				GL11.glColor3f(0f,0f,1f);
				GL11.glBegin(GL11.GL_QUADS);
				GL11.glVertex3f(0f, 0.0f, -0.2f);
				GL11.glVertex3f(0f, 0.0f, 0.2f);
				GL11.glVertex3f(0f, 0.5f, 0.2f);
				GL11.glVertex3f(0f, 0.5f, -0.2f);
				GL11.glEnd();
				GL11.glBegin(GL11.GL_TRIANGLES);
				GL11.glVertex3f(0f, 0.0f, -0.2f);
				GL11.glVertex3f(0f, 0.0f, 0.2f);
				GL11.glVertex3f(0.1f, 0f, 0.0f);
				GL11.glEnd();
			} else if(codice[0].equals("door_up")) {
		GL11.glDisable(GL11.GL_TEXTURE_2D);
				GL11.glColor3f(0f,0f,1f);
				GL11.glBegin(GL11.GL_QUADS);
				GL11.glVertex3f(-0.2f, 0.0f, 0.0f);
				GL11.glVertex3f(0.2f, 0.0f, 0.0f);
				GL11.glVertex3f(0.2f, 0.5f, 0.0f);
				GL11.glVertex3f(-0.2f, 0.5f, 0.0f);
				GL11.glEnd();
				GL11.glBegin(GL11.GL_TRIANGLES);
				GL11.glVertex3f(-0.2f, 0.0f, 0.0f);
				GL11.glVertex3f(0.2f, 0.0f, 0.0f);
				GL11.glVertex3f(0f, 0f, -0.1f);
				GL11.glEnd();
			} else if(codice[0].equals("door_dn")) {
		GL11.glDisable(GL11.GL_TEXTURE_2D);
				GL11.glColor3f(0f,0f,1f);
				GL11.glBegin(GL11.GL_QUADS);
				GL11.glVertex3f(-0.2f, 0.0f, 0.0f);
				GL11.glVertex3f(0.2f, 0.0f, 0.0f);
				GL11.glVertex3f(0.2f, 0.5f, 0.0f);
				GL11.glVertex3f(-0.2f, 0.5f, 0.0f);
				GL11.glEnd();
				GL11.glBegin(GL11.GL_TRIANGLES);
				GL11.glVertex3f(-0.2f, 0.0f, 0.0f);
				GL11.glVertex3f(0.2f, 0.0f, 0.0f);
				GL11.glVertex3f(0f, 0f, 0.1f);
				GL11.glEnd();
			} else {
		GL11.glDisable(GL11.GL_TEXTURE_2D);
				GL11.glBegin(GL11.GL_QUADS);
				GL11.glVertex3f(-0.1f, 0.0f, 0.0f);
				GL11.glVertex3f(0.1f, 0.0f, 0.0f);
				GL11.glVertex3f(0.1f, 0.05f, 0.0f);
				GL11.glVertex3f(-0.1f, 0.05f, 0.0f);
				GL11.glEnd();
			}
			GL11.glPopMatrix();
		}

		Display.update();
	}

	public void start() {
		try {
			Display.setTitle("Alone in the Game - by yuredd 2012");
			Display.setDisplayMode(new DisplayMode(800,600));
			Display.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
		}

		gameInit();

		while (!Display.isCloseRequested()) {

			gameLogic();

			gameGraphics();

		}

		Display.destroy();
	}

	public static void main(String[] argv) {
		Game game = new Game();
		game.start();
	}

}
